package com.icss.controller;

import com.icss.dao.CityMapper;
import com.icss.pojo.CityPojo;
import com.icss.pojo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CityController {

    @Autowired
    CityMapper mapper;

    @GetMapping("/getAllCity.action")
    public R getAll() {

        List<CityPojo> clist = mapper.findAllData();
        return R.success(clist);
    }

    @GetMapping("/getCityByName.action")
    @Transactional(readOnly = true)
    public R getCityByName(String city) {

        List<CityPojo> clist0 = mapper.findByCity(city);
        List<CityPojo> clist = mapper.findByCity(city);
        return R.success(clist);
    }

    @Autowired
    RedisTemplate redisTemplate;

    public static final String KEY01 = "key01";


    @GetMapping("/getAllCity0.action")
    public R getAll1() {
        List<CityPojo> clist0 = null;
        if (redisTemplate.hasKey(KEY01)) {
            clist0 = (List<CityPojo>) redisTemplate.opsForValue().get(KEY01);
        } else {
            clist0  = mapper.findAllData();
            redisTemplate.opsForValue().set(KEY01, clist0);
        }
        return R.success(clist0);
    }
}
